package com.lbb.springboot.util;

import javax.imageio.ImageIO;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;

public class ImageUtils {

    /**
     * @param args
     * @throws SQLException
     */
    public static void main(String[] args) throws SQLException {
        /*
         * String dbname=Cfg.getPropertiesVal("dbname"); String
         * dto=Cfg.getPropertiesVal("dto");
         */
//
        drawImage("D:/image.jpg",1);
    }

    public static void test() {
        int width = 595;
        int height = 842;

        String[] ss = {"道路运输车辆卫星定位装置安装证明",
                "兹有         广州市安豪物流有限公司        车牌牌号          粤ADG719       车牌",
                "颜色      黄色       ，已于（■      2018      年    03    月     20    日   □车辆出厂",
                "前）安装（      ■ 北斗/GPS双模         □ GPS）卫星定位装置，终端型号为",
                "NG-BC/NB.30G (为第  三  批符合道路运输车辆卫星定位系统标准的车载终",
                "端，生产厂家名称为  欣新科技（深圳）有限公司  ，厂家编号为  70214  ，",
                "车载终端序列号为 014045533030  ）SIM卡号为   1440045533030，采用",
                "巡航卫士GPS系统平台        (第  七  批符合道路运输卫星定位系统标准的系",
                "统平台，平台编号   51149   ）作为企业监控平台，并已接入政府监督平台，",
                "运行正常。",
                "特此证明。",
                "深圳市北斗时空科技有限公司",
                "2018 年 10 月 09 日"
        };

        File file = new File("D:/image.jpg");

        Font font = new Font("Serif", Font.BOLD, 20);
        //创建一个画布
        BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //获取画布的画笔
        Graphics2D g2 = (Graphics2D) bi.getGraphics();

        //开始绘图
        g2.setBackground(Color.WHITE);
        g2.clearRect(0, 0, width, height);
        g2.setPaint(Color.black);
        FontRenderContext context = g2.getFontRenderContext();


        Rectangle2D bounds = font.getStringBounds(ss[0], context);
        double x = width / 2 - (ss[0].length() * 5);
        double y = 100 + 1 * 20;
        double ascent = -bounds.getY();
        double baseY = y + ascent;
        //绘制字符串
        g2.drawString(ss[0], (int) x, (int) baseY);

        for (int i = 1; i < ss.length - 2; i++) {
            Rectangle2D boundsS = font.getStringBounds(ss[i], context);
            double xx = width / 2 - 200;
            double yy = 150 + i * 25;
            double ascentT = -boundsS.getY();
            double baseYY = yy + ascentT;

            //绘制字符串
            g2.drawString(ss[i], (int) xx, (int) baseYY);
        }

        for (int i = ss.length - 2; i < ss.length; i++) {
            Rectangle2D boundsS = font.getStringBounds(ss[i], context);
            double xx = width - 265;
            double yy = 170 +  i * 25;
            double ascentT = -boundsS.getY();
            double baseYY = yy + ascentT;

            //绘制字符串
            g2.drawString(ss[i], (int) xx, (int) baseYY);
        }


        try {
            //将生成的图片保存为jpg格式的文件。ImageIO支持jpg、png、gif等格式
            ImageIO.write(bi, "jpg", file);
        } catch (IOException e) {
            System.out.println("生成图片出错........");
            e.printStackTrace();
        }

    }


    public static void drawImage(String fileName, int count) {
        FileInputStream fin = null;
        try {
            DocFlavor dof = null;
            if (fileName.endsWith(".gif")) {
                dof = DocFlavor.INPUT_STREAM.GIF;
            } else if (fileName.endsWith(".jpg")) {
                dof = DocFlavor.INPUT_STREAM.JPEG;
            } else if (fileName.endsWith(".png")) {
                dof = DocFlavor.INPUT_STREAM.PNG;
            }

            PrintService ps = PrintServiceLookup.lookupDefaultPrintService();

            PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
            pras.add(OrientationRequested.PORTRAIT);

            pras.add(new Copies(count));
//            pras.add(PrintQuality.HIGH);
            DocAttributeSet das = new HashDocAttributeSet();

            // 设置打印纸张的大小（以毫米为单位）
            das.add(new MediaPrintableArea(0, 0, 210, 296, MediaPrintableArea.MM));
            fin = new FileInputStream(fileName);

            Doc doc = new SimpleDoc(fin, dof, das);

            DocPrintJob job = ps.createPrintJob();

            job.print(doc, pras);
            fin.close();
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (PrintException pe) {
            pe.printStackTrace();
        }finally {
        }
    }


}
